package m.jclecai.com.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

import m.jclecai.com.R;
import m.jclecai.com.inter.OnResultListener;

/**
 * Created by hp on 2017/4/6.
 * 投注确定适配器
 */

public class BettingConfirmAdapter extends BaseAdapter implements View.OnClickListener {

    private final Context context;
    private final OnResultListener listener;
    //球数据
    private ArrayList<String> ballNumber;
    //玩法数据
    private ArrayList<String> mode;
    //注数金额数据
    private ArrayList<String> numMoney;

    public BettingConfirmAdapter(Context context, OnResultListener listener) {
        this.context = context;
        this.listener = listener;
    }

    public void setData(ArrayList<String> ballNumber, ArrayList<String> mode, ArrayList<String> numMoney) {
        this.ballNumber = ballNumber;
        this.mode = mode;
        this.numMoney = numMoney;
        //这里我们对其上面的三个集合进行一个倒叙排列；
        Collections.reverse(ballNumber);
        Collections.reverse(mode);
        Collections.reverse(numMoney);
    }

    @Override
    public int getCount() {
        return ballNumber.size();
    }

    @Override
    public Object getItem(int position) {
        return ballNumber.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.betting_confirm_item, null);
            holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
            holder.iv_delete.setOnClickListener(this);
            holder.tv_ball_number = (TextView) convertView.findViewById(R.id.tv_ball_number);
            holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode);
            holder.tv_number_money = (TextView) convertView.findViewById(R.id.tv_number_money);
            holder.iv_next = (ImageView) convertView.findViewById(R.id.iv_next);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.iv_delete.setTag(position);
        if (ballNumber.size() != 0) {
            holder.tv_ball_number.setText(ballNumber.get(position));
        }
        if (mode.size() != 0) {
            holder.tv_mode.setText(mode.get(position));
        }
        if (numMoney.size() != 0) {
            holder.tv_number_money.setText(numMoney.get(position));
        }

        return convertView;
    }

    @Override
    public void onClick(final View v) {
        v.setEnabled(false);
        Animation animation = AnimationUtils.loadAnimation(context,R.anim.slide_out_to_right);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                v.setEnabled(true);
                int position = (int) v.getTag();
                ballNumber.remove(position);
                mode.remove(position);
                numMoney.remove(position);
                notifyDataSetChanged();
                listener.onReturnResult(ballNumber.size() + "");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        ((View)v.getParent()).startAnimation(animation);
    }

    private class ViewHolder {
        ImageView iv_delete;
        TextView tv_ball_number;
        TextView tv_mode;
        TextView tv_number_money;
        ImageView iv_next;
    }
}
